package com.cssw.fyzb.busi.api.acct;


import com.cssw.fyzb.core.model.vo.acct.UserBankVo;
import com.cssw.fyzb.busi.service.acct.OutAcctService;
import com.cssw.fyzb.core.model.dodb.UserBank;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: yangyiqun
 * @Date: 2020/7/11 16:25
 */
@Function(description = "查询银行卡列表", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true)
})
@Component
public class ListBank implements Action {
    private static final Logger logger = LoggerFactory.getLogger(ListBank.class);

    @Autowired
    private OutAcctService outAcctService;

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");

        try {
            List<UserBank> userBankList = outAcctService.listBankById(userId);
            List<UserBankVo> userBankVos = new ArrayList<>();
            UserBankVo userBankVo = null;
            for (UserBank u : userBankList) {
                userBankVo = new UserBankVo();
                userBankVo.setBankId(u.getBankId());
                userBankVo.setBankName(u.getBankName());
                userBankVo.setCarNo(u.getCardNo());
                userBankVo.setCreateTime(u.getCreateTime());
                userBankVo.setDefaultFlag(u.getDefaultFlag());
                userBankVo.setOpenBank(u.getOpenBank());
                userBankVo.setRealName(u.getRealName());
                userBankVo.setUserId(u.getUserId());
                userBankVo.setUpdateTime(u.getUpdateTime());
                userBankVo.setProvince(u.getProvince());
                userBankVo.setCity(u.getCity());
                userBankVo.setBankCode(u.getBankCode());
                userBankVos.add(userBankVo);
            }
            return Response.success().put("list", userBankVos);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("获取银行卡失败");
        }
    }
}
